package com.charging.config.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.charging.config.config.BaseController;
import com.charging.config.result.ApiCode;
import com.charging.config.result.ApiResult;
import com.charging.service.UserService;
import com.charging.util.AuthSign;
import me.chanjar.weixin.common.api.WxConsts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created by Zhang on 2018/8/26.
 */
@Configuration
public class Appinterceptor extends BaseController implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token= request.getHeader("token");

        HttpSession session = request.getSession();
        String token = (String) session.getAttribute("appLoginResult");

        if(!AuthSign.verify(token)){
            renderJson(JSONObject.toJSONString(ApiResult.fail(ApiCode.LOGIN_OUT)));
            return  false;
        };
        return  true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
